GBML manual / Maths

max_int

The same as max, for whole numbers: the larger of a and b, and the answer is a whole number. Use this one whenever the numbers are counted rather than measured - a lives count, a slot number, a picture number - so the variable you keep it in stays a whole number.

Syntax

max_int(a, b) -> int

Arguments

NameType
aint
bint

Returns

int

Description

The same as max, for whole numbers: the larger of a and b, and the answer is a whole number (so is max's, given whole numbers).

Reach for this one whenever the numbers are counted rather than measured: a lives count, a slot number, which picture of an animation to show. Use max for things that are measured - a position, a speed, a volume.

Example

var strip = max_int(int_floor(x / 32), 0);

Works out which 32-pixel-wide strip of the room this object is standing in, and never lets it go below strip 0, so an object that has wandered off the left edge still counts as strip 0 and not strip -1. strip is a whole number, so it can be handed to anything that wants one.

Why there are two of these

max and max_int do the same arithmetic, and given two whole numbers max already answers a whole number - so picture = max(picture - 1, 0); keeps picture a whole number. The only difference left is strictness: max_int refuses a decimal, so a measured number cannot slip into a counted one unnoticed. (clamp is different - it still answers a decimal; see clamp_int.)

Both numbers have to be whole

max_int takes whole numbers only. A number with a point in it is a real - even 4.0 - and is refused when the project is checked:

Argument 2 to `max_int` should be an int, but this is a real.

If you have a real and want a whole number out of it, round it first with int_floor, int_ceil or int_round. If the numbers really are measured, use max instead.

A whole number is also free of a real's limit: a real runs out at about 524,000 and a whole number does not run out until about 2.1 billion, so max_int(600000, 700000) is 700000.

Cost

One comparison - as cheap as this group gets, safe to call every frame.

On the GBA

max and max_int do the same arithmetic. Given two whole numbers, max already hands back a whole number, so max_int is now just a stricter spelling: it refuses a decimal outright, which catches a measured number slipping into a counted one. Both numbers have to be whole: max_int(n, 0.5) is refused when the project is checked, and so is a variable that holds a real, even one that happens to hold 4.0 - round it first with int_floor, int_ceil or int_round. One comparison, cheap enough for every frame.

See also: maxmin_intclamp_intint_floor